var _now = new Date(),
  now=new Date(_now.getTime() + 24*60*60*1000),
  app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img_src:app.img_src,
    list: [],
    date:'',
    now:'',
    time:{},
    toast:false,
    start:'',
    end:'',
    project:{},
    notime:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (o) {
    var t = this,
      date = app.helper.newTime(now);
    this.setData({
      project:JSON.parse(o.item),
      date:date[0],
      now:date,
      _now:app.helper.newTime(_now)
    },function () {
      t.getList()
    })
  },
  getList(){
    wx.showLoading()
    var t = this,p = t.data.project;
    wx.request({
      url:'https://live.sgwxmini.com/beauty/rowNumber/newYYList',
      data:{
        hosId:p.hos_id,
        itemId:p.item_id,
        itemDetailId:p.id,
        use_time:p.use_time,
        yyTime:t.data.date
      },
      success(e){
        console.log(e)
        if(e.data.result == 0){
          var notime = e.data.data.notime;
          t.setData({
            notime:notime || [],
            list:e.data.data.itemList.map(i=>{
              i.s = i.start.slice(11,16)
              i.e = i.end.slice(11,16)
              // if(notime.length && i.start.slice(11) == notime[0].begin_time && i.end.slice(11) == notime[0].end_time){
              //   i.noShow = true
              // }
              // i.adj = t.expire(i.e,p.use_time)
              return i
            })
          })
        }
        wx.hideLoading()
      }
    })
  },
  bindDateChange: function (e) {
    var t = this;
    this.setData({
      date: e.detail.value
    },t.getList)
  },
  bindTimeChange: function(e) {
    var t = this, d = e.detail.value;
    console.log(d)
    var notime = t.data.notime.some(i=>t.expire2(i.begin_time,i.end_time,d))
    var notime3 = t.expire3(t.data.time.e,d);
    if(notime || notime3){
      wx.showToast({icon:'none',title:'当前时间不可预约'});
      return false;
    }

    if(t.expire(d,-1)){
      this.setData({
        start:d,
        end:t.getEnd(d)
      })
      return true;
    }else {
      wx.showToast({icon:'none',title:'该时间段已经过期，请选择靠后的时间预约'})
      return false;
    }

  },
  expire:function(t,s){
    var  d = this.data, hm = new Date(d.date.replace(/-/g,'/')+' '+t);
    hm.setMinutes(hm.getMinutes() - s);
    return hm > _now;
  },
  expire2:function(s,e,d){
    var _t = this.data, t = _t.date.replace(/-/g,'/'),
      _s = new Date(t+' '+s),
      _e = new Date(t+' '+e),
      _d = new Date(t+' '+d);
    _s.setMinutes(_s.getMinutes() - _t.project.use_time);
    return _d>_s && _d<_e;
  },
  expire3:function(e,d){
    var _t = this.data,
      t = _t.date.replace(/-/g,'/'),
      _d =  new Date(t+' '+d),
      _e = new Date(t+' '+e);
    _e.setMinutes(_e.getMinutes() - _t.project.use_time);
    // console.log(d,e,_d.toLocaleTimeString(),_e.toLocaleTimeString(),_d > _e)
    return _d > _e || _d == _e
  },
  setTime:function(e){
    var t = this, item = e.currentTarget.dataset.item;
    // item.adj && this.setData({
    item.isBlank && this.setData({
      toast:true,
      time:{s:item.s, e:item.e},
      start:item.s,
      end:t.getEnd(item.s)
    })

  },
  clearToast:function(){
    this.setData({toast:false})
  },
  getEnd(t){
    var d=this.data, start = d.date.replace(/-/g,'/') +' '+ t,
      _time = new Date(start),
      time = new Date(_time.getTime() + d.project.use_time *60*1000 );
    return app.helper.newTime(time)[1];
  },
  mySubmit:function(){
    var t =this, d = t.data,
      p = d.project,
      pages = getCurrentPages(),
    prevPage = pages[pages.length - 3];

    if(!t.bindTimeChange({detail:{value:d.start}})) return;

    p.s = d.date+' '+d.start;
    p.e = d.date+' '+d.end;

    d.end ? prevPage.setData({item:p},function () {
      wx.navigateBack({delta:2});
    }) : wx.showToast({icon:'none',title:'请选择预约时间'})
  },
})